<!--
/// 
/// MaDAson - Toolkit for Model Driven Software Development
///
/// Copyright [2006] Marcus Lankenau
/// 
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///  
///     http://www.apache.org/licenses/LICENSE-2.0
///  
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
/// 
-->
<%@ Page Language="C#" %>
<%@ Import Namespace="Madason.MetaModel" %>
<%@ Import Namespace="Madason.MetaModel.DAO" %>
package <%= Class.Namespace.FullName %>;

import java.util.*;
import java.sql.*;
import com.ibatis.sqlmap.client.SqlMapClient;

/**
 * <%= Class.Documentation.Replace("\n", "\n	* ") %>
 */
public class <%= Class.Name %>AccessIBatis extends <%= Class.Name %>Access
{
<% if (!Class.Readonly)
   { %>	
	
    /**
     * insert new <%= Class.Name%>
     */    
	public void insert(Connection conn, <%= Class.Name%> bean) throws Exception
	{
		SqlMapClient sqlMap = SqlMapSingleton.getSqlMapClient();
		sqlMap.setUserConnection(conn);
		sqlMap.insert("<%= Class.Name %>.insert", bean);
	}	

	public void delete(Connection conn, <%= PrimaryKey.Type.Name%> primaryKey) throws Exception
	{			
	}


	public void update(Connection conn, <%= Class.Name%> bean) throws Exception
	{
		SqlMapClient sqlMap = SqlMapSingleton.getSqlMapClient();
		sqlMap.setUserConnection(conn);
		sqlMap.update("<%= Class.Name %>.update", bean);
	}
		
	
	<% foreach (PersistedAssociation assoc in Class.ComplexAssociations)
	{ %>
	<% string oppositeKey = (assoc.TargetClass as Entity).PrimaryKey.NameCamelCase; %>
	/// <summary>
	/// n-n Beziehung '<%= assoc.RoleName%>' updaten
	/// </summary>
	/// <returns></returns>
    public void update<%= StringUtil.MakePascalCase(assoc.RoleName)%>(Connection conn, int <%= PrimaryKey.NameCamelCase%>, List<%= "<" + assoc.TargetClass.ExplicitName + ">"%> <%= assoc.RoleName%>)
    {
    }


	/// <summary>
	/// n-n Beziehung '<%= assoc.RoleName%>' updaten
	/// </summary>
	/// <returns></returns>
    public void update<%= StringUtil.MakePascalCase(assoc.RoleName)%>ByIds(Connection conn, Integer <%= PrimaryKey.NameCamelCase%>, List<%= "<Integer>"%> <%= assoc.RoleName%>)  throws Exception
    {
    }

    <% } %>

		
	<% if (Class.Attributes.Count == 1 && Class.ActiveAssociations.Count == 2 && Class.SuperClass == null)
	{ %>
	<%   foreach (PersistedAssociation assoc in Class.ActiveAssociations)
	  { %>
	<%     Association multyPart = (assoc == Class.ActiveAssociations[0]) ? Class.ActiveAssociations[1] : Class.ActiveAssociations[0]; %>
	public override void UpdateMultiple<%= multyPart.Name%>For<%= assoc.RoleName%>(List<%= "<int>"%> <%= multyPart.Name%>, int <%= assoc.RoleName%>)
	{
	}
	<%   } %>		
	<% } %>
		
<%  } %>		
		
	<% foreach (PersistedAssociation assoc in Class.ComplexAssociations) { %>

	/// <summary>
	/// n-n Beziehung zu <%= assoc.TargetClass.Name %>
	/// </summary>
	/// <returns></returns>
	public List<%= "<" + assoc.TargetClass.ExplicitName + ">" %> find<%= StringUtil.MakePascalCase(assoc.RoleName) %>(<%= PrimaryKey.Type.ExplicitName %> <%= PrimaryKey.Name %>)
	{
	}
	<% } %>
	
	<% foreach (Finder finder in Class.GetAndExpandFinder()) {	%>
	public <%= OperationReturnType(finder) %> <%= finder.Name %>(Connection conn<%= FinderParamSep(finder) + finder.ParameterString %>) throws Exception
	{
        HashMap params = new HashMap();
<% foreach (Parameter param in finder.Parameters) { %>
		params.put("<%= param.Name %>", <%= param.Name %>);
<% } %>    
    
<% if (finder.ReturnType.Name.ToLower() == "list") { %>
        SqlMapClient sqlMap = SqlMapSingleton.getSqlMapClient();
        sqlMap.setUserConnection(conn);
		List genList = sqlMap.queryForList("<%= Class.Name %>.<%= finder.Name %>", params);
		List<<%= Class.Name %>> result = new ArrayList<<%= Class.Name %>>();			
		for (Object obj : genList)
			result.add((<%= Class.Name %>)obj);
        return result;            
<% } else { %>
		SqlMapClient sqlMap = SqlMapSingleton.getSqlMapClient();                
		sqlMap.setUserConnection(conn);
		return (<%= Class.Name %>) sqlMap.queryForObject("<%= Class.Name %>.<%= finder.Name %>", params);
<% } %>
	}		
	
	
	<% } %>
}
  

	





<script runat="server">
	private Entity @class;


	public PersistedAttribute PrimaryKey
	{
		get
		{
			return Class.PrimaryKey;
		}
	}

	public PersistedAttribute VersionAttribute
	{
		get { return Class.GetAttribute(Stereotype.Version) as PersistedAttribute; }
	}	
	
	

	public Entity Class
	{
		get { return @class; }
		set { @class = value; }
	}

	protected string OperationReturnType(Operation finder)
	{
		return (finder.ReturnType.ExplicitName == "List") ?
		   "List<" + Class.Name + ">" :
		   finder.ReturnType.Name;
	}

	protected ClassList ClassesForFinder
	{
		get
		{
			ClassList classesForFinder = new ClassList();
			classesForFinder.AddRange(Class.SuperClassesPath);
			classesForFinder.Add(Class);
			classesForFinder.AddRange(Class.SubClassesDeep);
			return classesForFinder;
		}
	}
	
	protected string FinderParamSep(Finder finder)	
	{
		return finder.Parameters.Count > 0 ? ", " : "";
	}	
</script>
